//
//  Tool.swift
//  weather
//
//  Created by 陈思斌 on 26/8/17.
//  Copyright © 2017年 陈思斌. All rights reserved.
//

import UIKit
class Tool {
    class  func returnDate(date:NSDate)->String{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd"
        dateFormatter.locale = NSLocale(localeIdentifier: "cn") as Locale!
        return dateFormatter.string(from: date as Date)
    }
    
    class func returnWeekDay(date:NSDate)->String{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        dateFormatter.locale = NSLocale(localeIdentifier: "cn") as Locale!
        let dateStr = dateFormatter.string(from: date as Date)
        switch dateStr {
        case "Monday":
            return "周一"
        case "Tuesday":
            return "周二"
        case "Wednesday":
            return "周三"
        case "Thursday":
            return "周四"
        case "Friday":
            return "周五"
        case "Saturday":
            return "周六"
        default:
            return "周日"
        }
    }
  
    class func colorWithHexString(Hex:String) -> UIColor{
        var cString = Hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        if cString.hasPrefix("#"){
            cString = (cString as NSString).substring(from: 1)
        }
        
        if cString.characters.count != 6{
            return UIColor.gray
        }
        
        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
    }
    
    class func returnWeatherBgColor(weatherType:String)->UIColor{
        let weatherTypePath = Bundle.main.path(forResource: "weatherBG", ofType: "plist")
        if weatherTypePath != nil{
            let json = NSDictionary(contentsOfFile: weatherTypePath!)
            for element in (json?.allKeys)!{
                if element as! String == weatherType || weatherType.hasPrefix(element as! String){
                    let key = element as! String
                    let value = json![key] as! String
                    return Tool.colorWithHexString(Hex: value)
                }
                
            }
        }
        return UIColor.blue
    }
    
    class func returnNeedDateType(getDateString:String)->String{
        let dateFormatter = DateFormatter()
        dateFormatter.locale = NSLocale(localeIdentifier: "cn") as Locale
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let date = dateFormatter.date(from: getDateString)
        let newDataFormatter = DateFormatter()
        newDataFormatter.locale = NSLocale(localeIdentifier: "cn") as Locale
        newDataFormatter.dateFormat = "MM/dd"
        let dateStr = newDataFormatter.string(from: date!)
        return dateStr
    }
    
    class func returnNeedWeekDayType(getWeekDayString: String)->String{
        switch getWeekDayString {
        case "星期一":
            return "周一"
        case "星期二":
            return "周二"
        case "星期三":
            return "周三"
        case "星期四":
            return "周四"
        case "星期五":
            return "周五"
        case "星期六":
            return "周六"
        default:
            return "周日"
        }
    }
    
    class func returnWeatherImage(weatherType:String)->UIImage?{
        let weatherTypePath = Bundle.main.path(forResource: "weatherImage", ofType: "plist")
        if weatherTypePath != nil{
            let json = NSDictionary(contentsOfFile: weatherTypePath!)
            for element in (json?.allKeys)!{
                if element as! String == weatherType || weatherType.hasPrefix(element as! String){
                    let key = element as! String
                    let value = json![key] as! String
                    return UIImage(named: value)
                }
                
            }
        }
        return nil
    }
    
    class func returnMessageImage(curWeatherInfo:WeatherInfo)->UIImage?{
        let temp = NumberFormatter().number(from: curWeatherInfo.temp_low!)
        if Int(temp!) > 30{
            return UIImage(named: "jrgw_normal")
        }
        
        let weatherTypePath = Bundle.main.path(forResource: "weatherMessage", ofType: "plist")
        if weatherTypePath != nil{
            let json = NSDictionary(contentsOfFile: weatherTypePath!)
            for element in (json?.allKeys)!{
                if (curWeatherInfo.weather?.hasPrefix(element as! String))!{
                    let value = json?.value(forKey: element as! String) as! String
                    return UIImage(named: value)
                }
            }
        }
        return nil
    }
//    将某个view 转换成图像
    class func getImageFromView(view:UIView) ->UIImage{
        UIGraphicsBeginImageContext(view.bounds.size)
        
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return image!
    }
}
